package com.zw.mizhe.view.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.zw.mizhe.App;
import com.zw.mizhe.R;
import com.zw.mizhe.api.ExceptionHandle;
import com.zw.mizhe.bean.base.BaseDataEntity;
import com.zw.mizhe.bean.base.SplashAdsEntity;
import com.zw.mizhe.common.base.mvp.MvpActivity;
import com.zw.mizhe.common.net.NetWorkUtil;
import com.zw.mizhe.common.utils.AppUtils;
import com.zw.mizhe.presenter.SplashPresenter;
import com.zw.mizhe.view.impl.SplashView;

import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.functions.Action1;

public class SplashActivity extends MvpActivity<SplashView, SplashPresenter> implements SplashView {
    SharedPreferences sp;
    public static final String FIRST_AD_IS_HAVE_PREFS = "firstAd";
    public static final String FIRST_AD_PATH_PREFS = "adPath";

    private boolean isFristRun = true;

    @NonNull
    @Override
    public SplashPresenter createPresenter() {
        return new SplashPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }


    @Override
    public void before() {
        sp = PreferenceManager.getDefaultSharedPreferences(this);
        isFristRun = AppUtils.isFristRun(this);
    }

    @Override
    public void initView() {
        Observable.timer(1, TimeUnit.SECONDS).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                showLaunchActivity();
            }
        });
    }

    public void showLaunchActivity() {
        if (isFristRun) {
            startActivity(new Intent(this, GuideActivity.class));
        } else {
            if (NetWorkUtil.isNetworkConnected(this)) {
                mPresenter.loadBaseData();
            } else {
                startActivity(new Intent(this, MainActivity.class));
            }
        }
    }

    @Override
    public void onSuccess(BaseDataEntity entity) {
        App.getInstance().entity = entity;
        if (entity != null) {
            List<SplashAdsEntity> splashAds = entity.getSplashAds();
            if (splashAds != null && !splashAds.isEmpty()) {
                String img = splashAds.get(0).getImg();
                Glide.with(App.getInstance())
                        .load(img)
                        .downloadOnly(new SimpleTarget<File>() {
                            @Override
                            public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
                                sp.edit().putString(FIRST_AD_PATH_PREFS, resource.getAbsolutePath()).apply();
                            }
                        });
            } else {
                sp.edit().putBoolean(FIRST_AD_IS_HAVE_PREFS, false).apply();
            }
        }
        showActivity(AdActivity.class);
    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {

    }

    @Override
    public void onFinish() {

    }

    public void showActivity(Class<?> cls) {
        startActivity(new Intent(this, cls));
        onBackPressed();
    }


}
